package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.UserConsigneeMapper;
import mobi.hami.huapin.model.UserConsignee;
import mobi.hami.huapin.service.UserConsigneeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by 小峰 on 2018/8/23.
 */
@Service
public class UserConsigneeServiceImpl implements UserConsigneeService{

    @Autowired
    private UserConsigneeMapper userConsigneeMapper;
    @Override
    public int save(UserConsignee userConsignee) {
         userConsigneeMapper.insertSelective(userConsignee);
         return userConsignee.getId();
    }

    @Override
    public int update(UserConsignee userConsignee) {
        UserConsignee userConsignee1=selectOnById(userConsignee.getId());
        if(userConsignee.getName().equals(userConsignee1.getName())
                && userConsignee.getPhone().equals(userConsignee1.getPhone()))
        {
            return  userConsignee.getId();
        }
        deleteById(userConsignee1.getId());
        userConsignee.setId(null);
        return  save(userConsignee);
    }

    @Override
    public int deleteById(Integer id) {
        UserConsignee userConsignee= new UserConsignee();
        userConsignee.setId(id);
        userConsignee.setIs_valid(0);
        return userConsigneeMapper.updateByPrimaryKeySelective(userConsignee);
    }

    @Override
    public UserConsignee selectOnByUserid(Integer id) {
        UserConsignee userConsignee= new UserConsignee();
        userConsignee.setUser_id(id);
        userConsignee.setIs_valid(1);
        return userConsigneeMapper.selectOne(userConsignee);
    }

    @Override
    public UserConsignee selectOnById(Integer id) {
        return userConsigneeMapper.selectByPrimaryKey(id);
    }
}
